package com.hospital.warehouse.outstock.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hospital.registermanagement.entity.Pharmacy;
import com.hospital.util.DTO;
import com.hospital.warehouse.drugdicts.entity.Drugdicts;
import com.hospital.warehouse.drugdicts.service.DrugdictsService;
import com.hospital.warehouse.exception.WarehouseException;
import com.hospital.warehouse.instock.entity.Instock;
import com.hospital.warehouse.instock.service.InstockService;
import com.hospital.warehouse.outstock.entity.Outstock;
import com.hospital.warehouse.outstock.service.OutstockService;
import com.hospital.warehouse.storehouse.entity.Storehouse;
import com.hospital.warehouse.storehouse.service.StorehouseService;
import com.hospital.warehouse.sys_user.entity.Sys_user;
import com.hospital.warehouse.sys_user.service.Sys_userService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mr. YR
 * @since 2020-04-20
 */
@Controller
@RequestMapping("/warehouse/outstock")
public class OutstockController {

	@Autowired
	OutstockService outstockService;
	@Autowired
	DrugdictsService drugdictsService;
	@Autowired
	Sys_userService sys_userService;
	@Autowired
	InstockService instockService;
	@Autowired
	StorehouseService storehouseService;

	@RequestMapping("/page")
	public String page(Model model) {
		// 添加药品集合，用于搜索下拉框
		List<Drugdicts> drugs = drugdictsService.list();
		model.addAttribute("drugs", drugs);
		// 添加用户集合，用于搜索下拉框
		List<Sys_user> users = sys_userService.list();
		model.addAttribute("users", users);

		// 药房集合
		List<Pharmacy> pharmacies = new ArrayList<Pharmacy>();
		Pharmacy e = new Pharmacy();
		e.setPharmacy_id(1);
		pharmacies.add(e);
		Pharmacy e2 = new Pharmacy();
		e2.setPharmacy_id(2);
		pharmacies.add(e2);
		model.addAttribute("pharmacies", pharmacies);

		// 添加库房集合，用于搜索下拉框
		List<Storehouse> storehouses = storehouseService.list();
		model.addAttribute("storehouses", storehouses);

		// 添加入库单集合，用于搜索下拉框
		List<Instock> instocks = instockService.list();
		model.addAttribute("instocks", instocks);

		// 添加出库集合，用于搜索选择
		List<Outstock> outstocks = outstockService.list();
		model.addAttribute("outstocks", outstocks);

		// 前往主页面
		return "views/warehouse/outstock.html";
	}

	@ResponseBody
	@RequestMapping("/paging")
	public DTO paging(Outstock po, Integer page, Integer limit) {
		DTO dto = new DTO();
		try {
			dto = outstockService.paging(po, page, limit);
		} catch (ParseException e) {
			e.printStackTrace();
			dto.setMsg("日期错误");
		}
		return dto;
	}

	@RequestMapping("/editpage")
	public String editpage(Model model, Integer id) {
		if (id != null) {
			Outstock po = outstockService.queryById(id);
			model.addAttribute("po", po);
		}

		List<Drugdicts> drugs = drugdictsService.list();
		model.addAttribute("drugs", drugs);
		List<Sys_user> users = sys_userService.list();
		model.addAttribute("users", users);
		// 药房集合
		List<Pharmacy> pharmacies = new ArrayList<Pharmacy>();
		Pharmacy e = new Pharmacy();
		e.setPharmacy_id(1);
		pharmacies.add(e);
		Pharmacy e2 = new Pharmacy();
		e2.setPharmacy_id(2);
		pharmacies.add(e2);
		model.addAttribute("pharmacies", pharmacies);

		return "views/warehouse/outstock_edit.html";
	}

	@ResponseBody
	@RequestMapping("/insertone")
	public Object insertOne(@RequestBody Outstock po, HttpSession session) {
		try {
			outstockService.saveOne(po, session);
		} catch (WarehouseException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "新增成功";
	}

	@ResponseBody
	@RequestMapping("/delete")
	public Object delete(@RequestParam("ids[]") Integer[] ids) {
		try {
//			System.out.println(Arrays.asList(ids));
			outstockService.removeByIds(Arrays.asList(ids));
		} catch (WarehouseException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "删除成功";
	}

	@ResponseBody
	@RequestMapping("/updateone")
	public Object updateOne(@RequestBody Outstock po) {
		try {
			outstockService.updateOne(po);
		} catch (WarehouseException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "修改成功";
	}
}
